#include "DbStorage/CreatePackageStorageCmd.hpp"
#include "CaseDB/Connection.hpp"
#include "CaseDB/Statement.hpp"
#include "Model/Package.hpp"
#include "Model/Project.hpp"
#include <cassert>

namespace DbStorage
{
	

CreatePackageStorageCmd::CreatePackageStorageCmd( Model::Package& subject, CaseDB::Connection& dbConnection )
: DbStorageCmd( subject, Storage::StorageCommand::Create, dbConnection)
{
}

// ---------------------------------------------------------------------------------------------------------------------

void CreatePackageStorageCmd::execute()
{
	using namespace CaseDB;

	Model::Package& package = dynamic_cast< Model::Package&>( rSubject_);
	assert( NULL != package.project());
	Statement insertStatement( "{?=call CREATE_PACKAGE( ?, ?, ?, ?)}");
	insertStatement.addParameter( DbTypesEnum::Integer);
	insertStatement.addParameter( DbTypesEnum::Integer, package.project()->storedID());
	insertStatement.addParameter( DbTypesEnum::Integer, (NULL == package.parentPackage()) ? boost::any() : package.parentPackage()->storedID());
	insertStatement.addParameter( package.name());
	insertStatement.addParameter( package.description());

	dbConnection_.execute( insertStatement);

	boost::any packageID = insertStatement.paramsBegin()->value();
	package.setStoredID( packageID);
}


} // namespace DbStorage
